package com.cyt.web.controller.sys;

import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.digest.DigestUtil;
import com.cyt.base.enums.BusinessType;
import com.cyt.base.enums.OperatorType;
import com.cyt.base.model.dto.ReturnResult;
import com.cyt.base.util.StringUtils;
import com.cyt.sys.aspect.BaseLog;
import com.cyt.sys.model.po.SysUser;
import com.cyt.sys.model.vo.SysUserTokenVo;
import com.cyt.sys.model.vo.SysUserVo;
import com.cyt.sys.service.SysUserService;
import com.cyt.sys.service.SysUserTokenService;
import com.cyt.web.controller.base.BaseController;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 系统模块-用户Token控制层
 *
 * @author congcong tao
 * @date 2019-4-24 9:38:22
 */
@RestController
public class SysUserTokenController extends BaseController {

    @Resource
    private SysUserTokenService sysUserTokenService;
    @Resource
    private SysUserService sysUserService;

    /**
     * 统一跳转
     *
     * @param model 数据模型
     * @return Model
     * @author congcong tao
     * @date 2019-4-24 9:38:22
     */
    @RequestMapping("/sys/sysUserToken/*")
    public Model goPage(Model model) {
        return model;
    }

    /**
     * 分页查询
     *
     * @param sysUserTokenVo 接受参数对象
     * @param page           当前页面
     * @param limit          页面数量
     * @return ReturnResult
     * @author congcong tao
     * @date 2019-4-24 9:38:22
     */
    @BaseLog(module = "系统模块", classz = "用户Token", methods = "分页查询对象", businessType = BusinessType.GET, operatorType = OperatorType.MANAGE)
    @RequestMapping(value = "/sys/sysUserToken/page", method = RequestMethod.POST)
    public ReturnResult page(SysUserTokenVo sysUserTokenVo, int page, int limit) {
        return sysUserTokenService.page(sysUserTokenVo, page, limit);
    }

    /**
     * 跳转添加或编辑界面
     *
     * @param id 主键
     * @return ReturnResult 对象
     * @author congcong tao
     * @date 2019-4-24 9:38:22
     */
    @RequestMapping(value = "/sys/sysUserToken/getAddOrEdit", method = RequestMethod.GET)
    public ModelAndView getAddOrEdit(String id) {
        ModelAndView view = new ModelAndView("/sys/sysUserToken/sysUserTokenEdit");
        if (StrUtil.isNotEmpty(id)) {
            view.addObject(sysUserTokenService.selectByPrimaryKey(id));
        }
        return view;
    }

    /**
     * 添加或更新
     *
     * @param request        对象
     * @param sysUserTokenVo 对象
     * @return ReturnResult 对象
     * @author congcong tao
     * @date 2019-4-24 9:38:22
     */
    @BaseLog(module = "系统模块", classz = "用户Token", methods = "保存或更新对象", businessType = BusinessType.INSERT, operatorType = OperatorType.MANAGE)
    @RequestMapping(value = "/sys/sysUserToken/addOrUpdate", method = RequestMethod.POST)
    public ReturnResult addOrUpdate(HttpServletRequest request, SysUserTokenVo sysUserTokenVo) {
        return sysUserTokenService.addOrUpdate(sysUserTokenVo, acceptCurrentUser(request));
    }

    /**
     * token登录
     *
     * @param userName 用户名称
     * @param password 用户密码
     * @author congcong tao
     * @date 2019/4/24 12:01
     */
    @BaseLog(module = "系统模块", classz = "用户Token", methods = "登录", businessType = BusinessType.INSERT, operatorType = OperatorType.MANAGE)
    @RequestMapping(value = "/sys/sysUserToken/login", method = RequestMethod.POST)
    public ReturnResult login(String userName, String password) {
        // 获取用户信息
        List<SysUser> list = sysUserService.selectList(new SysUserVo(userName));
        if (list == null || list.size() == 0) {
            return new ReturnResult(10, "未查询到该用户");
        }
        SysUser sysUser = list.get(0);
        String passwordMd5 = DigestUtil.md5Hex(password);
        if (StringUtils.equals(sysUser.getPassword(), passwordMd5)) {
            return sysUserTokenService.insertToken(sysUser.getId());
        } else {
            return new ReturnResult(10, "密码不正确");
        }
    }

    /**
     * 根据主键id集合软删除
     *
     * @param id 主键id集合，多个以‘，’隔开
     * @return ReturnResult 对象
     * @author congcong tao
     * @date 2019-4-24 9:38:22
     */
    @BaseLog(module = "系统模块", classz = "用户Token", methods = "删除对象", businessType = BusinessType.DELETE, operatorType = OperatorType.MANAGE)
    @RequestMapping(value = "/sys/sysUserToken/removeByIds", method = RequestMethod.GET)
    public ReturnResult removeByIds(HttpServletRequest request, String id) {
        return sysUserTokenService.deleteByIds(id, acceptCurrentUser(request));
    }
}